#ifndef TIMEOUT_H_
#define TIMEOUT_H_

#ifdef __cplusplus
extern "C" {
#endif

#include <time.h>

#ifdef __cplusplus
}
#endif

#include <cppunit/Exception.h>
#include <cppunit/SourceLine.h>

class Timeout {
public:
	Timeout();

	static void start(int timeInMiliseconds, CppUnit::SourceLine timeoutStartLine) throw (CppUnit::Exception);
	static void stop() throw (CppUnit::Exception);

private:
	typedef void SignalHandler(int);
	static SignalHandler * setSignalHandler(int signalID, SignalHandler * handler) throw (CppUnit::Exception);
	static void handler(int alarmSignalID) throw (CppUnit::Exception);

	static Timeout timeout;

	unsigned long timeInMiliseconds;
	CppUnit::SourceLine timeoutStartLine;
	timer_t timerID;
};

#define START_TIMEOUT(timeInMiliseconds) Timeout::start(timeInMiliseconds, CPPUNIT_SOURCELINE())
#define STOP_TIMEOUT() Timeout::stop()

#endif /* TIMEOUT_H_ */
